<?php

if (!defined('DIR_SEP')) {
    define('DIR_SEP', DIRECTORY_SEPARATOR);
}

if (!defined('MODULES_DIR')) {
    define('MODULES_DIR', dirname(__FILE__).DIR_SEP);
}

require_once(MODULES_DIR.'db.inc');

class Tag
{
	protected $db;
	public $id;
	public $tag;

	function __construct()
	{
		$this->db = DB::getInstance()->getDB();
	}

	function add($tag)
	{
		$result = mysql_query("select id from t_tag where tag = '$tag' limit 1",$this->db);
		
		if( !$result || mysql_num_rows($result) == 0)
		{
			$query = mysql_query("insert into t_tag (tag) values ('$tag')",$this->db);
			if(!$query)
				throw new Exception("cannot add tag");
				
			return mysql_insert_id();
		}
		else
		{
			$row = mysql_fetch_row($result);
			return $row[0];
		}
	}
	
	function delete($tagId)
	{
		try
		{
			mysql_query('BEGIN',$this->db);
			$query = mysql_query("delete from t_feed_tag_item where tag_id =".$tagId
									,$this->db);
			if(!$query)
				throw new Exception("cannot delete tag feed relation");

			$query = mysql_query("delete from t_blog_post_tag where tag_id =".$tagId
									,$this->db);
			if(!$query)
				throw new Exception("cannot delete tag blog relation");
				
			$query = mysql_query("delete from t_tag where id = ".$tagId,$this->db);			

			if(!$query)
				throw new Exception("cannot delete tag");
			
			mysql_query('COMMIT',$this->db);
		}
		catch(Exception $ex)
		{
			mysql_query('ROLLBACK',$this->db);
		}
	}
	
	static function getAll()
	{
		return DB::getInstance()->query("select id, tag from t_tag");	
	}
	
	static function getFeedCount()
	{
		return DB::getInstance()->query("SELECT tt.id, tt.tag, count( tfi.id ) 
			FROM t_tag tt
			INNER JOIN t_feed_tag_item tfti ON tfti.tag_id = tt.id
			INNER JOIN t_feed_item tfi ON tfi.feed_id = tfti.feed_id
			GROUP BY tfti.tag_id");	
	}

	static function getBlogCount()
	{
		$rows = DB::getInstance()->query(
				"SELECT t.tag,tbpt.tag_id,count(1) as articles, 1 as level
				FROM t_blog_post_tag tbpt
				inner join t_tag t on t.id = tbpt.tag_id
				group by tbpt.tag_id
				order by articles asc");
		
		if(count($rows)>5)
		{			
			for($i=0; $i<count($rows);$i++)
			{
				$level = floor($i/(count($rows)/5));
				$rows[$i]["level"] = $level == "" ? 0 : $level;
			}
		}
		else
		{
			foreach($rows as $row)
				$row["level"] = 0;
		}		
		
		//sorting by name
		$arr = Array();		
		foreach($rows as $row)
			$arr[$row['tag']] = $row;
		asort($arr);
				
		return $arr;
	}
	
	function getByFeed($feedId)
	{
		return DB::getInstance()->query(
			"select id, tag from t_tag tft
			inner join t_feed_tag_item tfti on tfti.tag_id = tft.id
			where feed_id = ".$feedId);	
	}

	static function getByBlogPost($postId)
	{
		return DB::getInstance()->query(
			"select id, tag from t_tag tft
			inner join t_blog_post_tag tbpt on tbpt.tag_id = tft.id
			where post_id = ".$postId);	
	}
	
	function load($tagId)
	{
		try
		{
			$sql ="select id, tag
					FROM t_tag 
					WHERE id = ".$tagId;

			$query = mysql_query($sql,$this->db);

			if (!$query)
				throw new Exception(mysql_error());
						
			$row = mysql_fetch_row($query);
							
			$this->id = $tagId;
			$this->tag = $row[1];
			
			return true;
		}
		catch(Exception $ex)
		{
			die($ex->getMessage());
			return false;
		}
	}
	
	function update()
	{
		try
		{
			$sql ="update t_tag 
					set tag = '".$this->tag."'
					WHERE id = ".$this->id;

			$query = mysql_query($sql,$this->db);

			if (!$query)
				throw new Exception(mysql_error());

			return true;
		}
		catch(Exception $ex)
		{
			die($ex->getMessage());
			return false;
		}
	}
	
	static function parseFreeText($tagsFreeText)
	{
		$result = Array();
		$txt = trim($tagsFreeText);
		$arr = explode(",",$txt);
		
		for($i=0;$i<count($arr);$i++)
		{
			$val = trim($arr[$i]);
			
			if(strlen($val) == 0)
				continue;
				
			$result[] = $val;
		}
		
		return $result;
	}
}